package com.bzkj.web.controller.ordermanagement.outbound;

import com.alibaba.fastjson2.JSON;
import com.bzkj.common.annotation.LogB;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.core.text.Convert;
import com.bzkj.common.domain.NccRequestDTO;
import com.bzkj.common.enums.BusinessType;
import com.bzkj.common.utils.ServletUtils;
import com.bzkj.document.domain.DocConfirmDTO;
import com.bzkj.document.domain.materialout.dto.IcMaterialRequest;
import com.bzkj.document.domain.materialout.dto.MaterialOutboundDTO;
import com.bzkj.document.service.materialsoutbound.MaterialOutboundService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

import static com.bzkj.common.core.page.TableSupport.PAGE_NUM;
import static com.bzkj.common.core.page.TableSupport.PAGE_SIZE;

/**
 * @projectName: prt-wms
 * @package: com.bzkj.web.controller.ordermanagement.outbound
 * @className: MaterialOutboundController
 * @description: 材料出库单控制层
 * @author: C1309
 * @createDate: 2024年12月09日
 * @version: v1.0
 */
@RestController
@RequestMapping("/materials-outbound")
public class MaterialOutboundController extends BaseController {

    @Resource
    private MaterialOutboundService materialOutboundService;

    /**
     * 查询NCC材料出库单
     *
     * @return 查询结果
     */
    @GetMapping("/ncc")
    @PreAuthorize("@ss.hasPermi('order:material:list')")
    public TableDataInfo selectNccMaterialOutbound(@RequestParam Map<String, Object> requestBody) {
        NccRequestDTO nccRequestDTO = getNccRequestDTO(requestBody);
        if (nccRequestDTO.getFbillflag() == null || nccRequestDTO.getFbillflag().isEmpty()) {
            nccRequestDTO.setFbillflag("2");
        }
        return materialOutboundService.selectMaterialOutboundOrder(nccRequestDTO);
    }

    /**
     * 新增材料出库单据
     *
     * @param responseBodyStr 单据数据
     * @return 新增结果
     */
    @PostMapping
    @PreAuthorize("@ss.hasPermi('order:material:add')")
    @LogB(title = "材料出库单", businessType = BusinessType.INSERT)
    public AjaxResult addMaterialOutboundOrder(@RequestBody String responseBodyStr) {
        IcMaterialRequest icMaterialRequest = JSON.parseObject(responseBodyStr, IcMaterialRequest.class);
        icMaterialRequest.setLoginUser(getLoginUser());
        return materialOutboundService.addMaterialOutboundOrder(icMaterialRequest);
    }

    /**
     * 修改材料出库单据
     *
     * @param responseBody 请求体
     * @return 修改结果
     */
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('order:material:edit')")
    @LogB(title = "材料出库单", businessType = BusinessType.UPDATE, pkFieldNames = "cgeneralhid",
            tableNames = "wms_warehouse_out_material",
            detailTable = "wms_warehouse_out_detail_material"
    )
    public AjaxResult updateOtherOutBoundOrder(@RequestBody HashMap<String, Object> responseBody) {
        return materialOutboundService.updateMaterialOutBoundOrder(responseBody, getLoginUser(), false);
    }

    /**
     * 确认NCC材料出库单
     *
     * @param docConfirmDTO NCC单据号
     * @return 确认结果
     */
    @PostMapping("/confirm")
    @PreAuthorize("@ss.hasPermi('order:material:confirm')")
    @LogB(title = "材料出库单", businessType = BusinessType.CONFIRM)
    public AjaxResult confirmOutboundOrder(@RequestBody DocConfirmDTO docConfirmDTO) {
        docConfirmDTO.setLoginUser(getLoginUser());
        return materialOutboundService.confirmOutboundOrder(docConfirmDTO);
    }

    /**
     * 查询本地材料出库单
     *
     * @param materialOutboundDTO 查询条件
     * @return 查询结果
     */
    @GetMapping
    @PreAuthorize("@ss.hasPermi('order:material:list')")
    public TableDataInfo selectMaterialOutboundOrder(MaterialOutboundDTO materialOutboundDTO) {
        if (materialOutboundDTO == null) {
            materialOutboundDTO = new MaterialOutboundDTO();
        }
        Integer pageNum = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
        return materialOutboundService.selectLocalMaterialOutboundOrder(materialOutboundDTO, pageNum, pageSize);
    }

    /**
     * 材料出库单签字
     *
     * @param cgeneralhid 单据表头主键
     * @return 查询结果
     */
    @PutMapping("/{cgeneralhid}")
    @PreAuthorize("@ss.hasPermi('order:material:audit')")
    @LogB(title = "材料出库单", businessType = BusinessType.SIGN)
    public AjaxResult signatureMaterialOutBound(@PathVariable("cgeneralhid") String cgeneralhid) {
        return materialOutboundService.signatureMaterialOutBound(cgeneralhid, getLoginUser());
    }

    /**
     * 材料出库单回退
     *
     * @param cgeneralhid 需要打回的单据类型
     * @return 回退结果
     */
    @PostMapping("/back/{cgeneralhid}")
    @PreAuthorize("@ss.hasPermi('order:material:edit')")
    @LogB(title = "材料出库单", businessType = BusinessType.CANCELED)
    public AjaxResult addPurchase(@PathVariable("cgeneralhid") String cgeneralhid) {
        return materialOutboundService.orderBack(cgeneralhid, getLoginUser());
    }

    /**
     * 失败库存处理
     *
     * @param cgeneralhid 表头主键
     * @return 处理结果
     */
    @PostMapping("/inventory/{cgeneralhid}")
    @PreAuthorize("@ss.hasPermi('order:material:audit')")
    @LogB(title = "材料出库单", businessType = BusinessType.INVENTORY)
    public AjaxResult updateInventory(@PathVariable("cgeneralhid") String cgeneralhid) {
        return materialOutboundService.updateInventory(cgeneralhid, getLoginUser());
    }

    /**
     * 删除材料出库单据
     *
     * @param header 单据表头主键
     * @param reason 作废原因
     * @return 删除结果
     */
    @DeleteMapping("/{header}")
    @PreAuthorize("@ss.hasPermi('order:material:delete')")
    @LogB(title = "材料出库单", businessType = BusinessType.INVALID)
    public AjaxResult deleteMaterialOutboundOrder(@PathVariable("header") String header,
                                                  @RequestParam(name = "reason", defaultValue = "") String reason) {
        if (reason == null || reason.trim().isEmpty()) {
            return AjaxResult.error("作废原因似乎没有填写，请填写作废原因后重试！");
        }
        return materialOutboundService.deleteMaterialOutboundOrder(header, getLoginUser(), reason);
    }
}
